<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
<!--      <el-select v-model="listQuery.roleType" class="filter-item" @change="getList(true)">-->
<!--        <el-option label="工作站" :value="1" ></el-option>-->
<!--        <el-option label="商家" :value="0"></el-option>-->
<!--      </el-select>-->
      <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入店铺名称"/>
      <el-button  class="filter-item" type="primary" icon="el-icon-search"
                 @click="getList(true)">查找
      </el-button>
      <el-button v-permission="['POST /admin/store/create']" class="filter-item" type="primary" icon="el-icon-edit"
                 @click="handleCreate">添加
      </el-button>
    </div>
    <!-- 查询结果 -->
    <el-table v-loading="list == undefined" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column width="60" align="center" label="ID" prop="id"/>
      <el-table-column prop="name" label="店铺名称" />
      <el-table-column prop="address" label="详细地址" />
      <el-table-column prop="phoneCode" label="手机号" show-overflow-tooltip />
      <el-table-column prop="contacts" label="联系人" show-overflow-tooltip />
      <el-table-column prop="storeType" label="店铺类型">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.storeType == 0 ? '普通' : '村社' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" width="70" prop="status" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status == 0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="isStatus($event,scope.row)">
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column prop="remark" label="描述" show-overflow-tooltip />
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['POST /admin/store/update']" type="primary" size="mini"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-permission="['POST /admin/store/delete']" type="danger" size="mini"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"/>
    <!-- 添加或修改对话框 -->
    <el-dialog :title="dataForm && (dataForm.id ? '编辑' : '创建')" :visible.sync="isDiglog">
      <add-form v-if="isDiglog" :dataForm="dataForm" :cancel="cancel"></add-form>
    </el-dialog>
  </div>
</template>

<style>
</style>

<script>
  import { list, remove, isStatus } from '@/api/apiShop'
  import AddForm from './add'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

  export default {
    name: 'Shop',
    components: { Pagination, AddForm },
    data() {
      return {
        dataForm: undefined,
        list: undefined,
        total: 0,
        isDiglog: false,
        listQuery: {
          page: 1,
          limit: 20,
          title: undefined,
          content: undefined,
          sort: 'add_time',
          order: 'desc',
          roleType: 0
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      isStatus(val,row) {
        let self = this
        isStatus({ids: row.id,status: val ? 0 : 1}).then(res => {
          row.status = val ? 0 : 1
        })
      },
      cancel(isRefresh = true) {
        this.isDiglog = false
        if(isRefresh){
          this.getList(true)
        }
      },
      getList(init = false) {
        if (init) {
          this.listQuery.page = 1
        }
        this.list = []
        this.total = 0
        list(this.listQuery)
          .then(response => {
            this.list = response.data.data.list
            this.total = response.data.data.total
          })
      },
      handleCreate() {
        // 判断超级管理员不能创建店铺
        if(this.$store.getters.adminType == 1){
          this.$notify.error({
            title: '警告',
            message: '请注册商家用户进行操作！'
          })
          return;
        }
        this.dataForm = {storeType: 0,villagesId: undefined}
        this.isDiglog = true
      },
      handleUpdate(row) {
        this.dataForm = row
        this.isDiglog = true
      },
      handleDelete(row) {
        remove({ id: row.id }).then(response => {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
      }
    }
  }
</script>
